package com.szsh.aiot.framework.influxdb;

import com.szsh.aiot.hsm.dto.camera.CameraAbnormal;
import com.szsh.aiot.hsm.utils.Hash163;
import org.influxdb.dto.Query;
import org.influxdb.impl.InfluxDBMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

@Component
public class CameraInfluxService {
    private static final Logger LOGGER = LoggerFactory.getLogger(CameraInfluxService.class);

    @Autowired
    private Hash163 hash;
    @Autowired
    private InfluxDBMapper influxMapper;

    /**
     * 获取摄像头设备3天内的异常信息
     *
     * @param phone
     * @param deviceMac
     * @param pushReason
     * @return
     */
    public List<CameraAbnormal> getAbnormalList(String phone, String deviceMac,
                                                String pushReason) throws Exception {
        StringBuilder querySB = new StringBuilder();
        querySB.append("select * from camera_abnormal where hashPhone='").append(hash.sHash(phone))
                .append("' and deviceMac='").append(deviceMac).append("'");
        if (!"0".equals(pushReason)) {
            querySB.append(" and pushReason=").append(pushReason);
        }
        querySB.append(" and time>now()-3d tz('Asia/Shanghai')");
        LOGGER.info("Camera influxDB query:{}", querySB.toString());
        Query query = new Query(querySB.toString());
        return influxMapper.query(query, CameraAbnormal.class);
    }

    /**
     * 查询指定时间内的摄像头异常信息
     *
     * @param phone
     * @param pushReason
     * @param startTime
     * @param endTime
     * @param deviceMac
     * @return
     */
    public List<CameraAbnormal> getListByTime(String phone, int pushReason, String startTime,
            String endTime, String deviceMac) throws Exception {
        StringBuilder querySB = new StringBuilder();
        querySB.append("select * from camera_abnormal where hashPhone='").append(hash.sHash(phone))
                .append("' and deviceMac='").append(deviceMac)
                .append("' and phone='").append(phone).append("'");
        if (pushReason != 0) {
            querySB.append(" and pushReason=").append(pushReason);
        }
        querySB.append(" and time>='").append(startTime).append("' and time<='").append(endTime);
        querySB.append("' tz('Asia/Shanghai')");
        LOGGER.info("Camera influxDB query:{}", querySB.toString());
        Query query = new Query(querySB.toString());
        return influxMapper.query(query, CameraAbnormal.class);
    }

    /**
     * 保存摄像头异常信息
     *
     * @param abnormal
     */
    public void saveAbnormal(CameraAbnormal abnormal) {
        influxMapper.save(abnormal);
    }

    /**
     * 获取用户摄像头最近一次异常访问
     *
     * @param phone
     * @return
     */
    public Optional<CameraAbnormal> getLastAbnormal(String phone) throws Exception {
        StringBuilder querySB = new StringBuilder();
        querySB.append("select * from camera_abnormal where hashPhone='").append(hash.sHash(phone))
                .append("' and phone='").append(phone)
                .append("' order by time desc limit 1 tz('Asia/Shanghai') ");
        LOGGER.info("Camera influxDB query:{}", querySB.toString());
        Query query = new Query(querySB.toString());
        List<CameraAbnormal> abnormalList = influxMapper.query(query, CameraAbnormal.class);
        if (abnormalList.isEmpty()) {
            return Optional.empty();
        }
        return Optional.of(abnormalList.get(0));
    }
}
